package GameMap;

import java.util.ArrayList;
import java.util.Observable;

import GameModel.mCard;
import GameModel.mGame;
import Message.XMLmessage;
import Player.mPlayer;
/**
 * 
 * @author Thomas Felder
 * @author Christopher Vogel
 */
public class mGameMap extends Observable {

	mGame mGame;
	ArrayList<mCard> selectedCards = new ArrayList<mCard>();

	public mGameMap(mGame mGame) {
		super();
		this.mGame = mGame;
		ServerListener.ServerListener.GameMap = this;
	}

	public void addChatMessage(XMLmessage msg) {
		System.out.println("addChatMessage in mGameMap"
				+ msg.getContent().getChat().toString());
		setChanged();
		notifyObservers(msg);

	}

	public void newCardCombo(XMLmessage msg) {
		try {
		System.out.println("mGamemap newCardCombo");
		ArrayList<mCard> cardcombo = msg.getContent().getCards();
		mPlayer player = msg.getSender();
		System.out.println("newCardCombo ist gegangen: "+mGame.newCardCombo(cardcombo, player)); // newCardCombo also should
												// change the handcards of its
												// player
		} catch (Exception e) {
			e.printStackTrace();
		}
		setChanged();
		notifyObservers(msg);
	}

	public void updateGameRoundContent(XMLmessage msg) {
		System.out.println("mGameMap updateHandCards");
		mGame.setHandCards(msg.getContent().getHandCardsList());
		mGame.setHaggis(msg.getContent().getHaggis());
		mGame.setCurrentPlayer(msg.getContent().getPlayer());
		setChanged();
		notifyObservers(msg);
	}

	public void updateBetsFromOpponent(XMLmessage msg) {
		System.out.println("mGameMap Bet");
		mGame.addBet(msg.getContent().getString(), msg.getSender());
		setChanged();
		notifyObservers(msg);
	}

	public ArrayList<mCard> getSelectedCards() {
		return selectedCards;
	}

	public void setSelectedCards(ArrayList<mCard> selectedCards) {
		this.selectedCards = selectedCards;
	}

	public void addPlayer(XMLmessage msg) {
		try {
			// if
			// (!msg.getSender().equals(ServerListener.ServerListener.Player)) {
			mGame.addPlayerKonto(msg.getSender());
			// }
			setChanged();
			notifyObservers(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void userExitWithoutLogout(XMLmessage msg) {
		if (!mGame.removePlayerKonto(msg.getSender())) {
			setChanged();
			notifyObservers(msg);
		}
	}
	
	public void updatePass(XMLmessage msg){
		mGame.addPassPlayer(msg.getSender());
		setChanged();
		notifyObservers(msg);
	}

	public void updateBombPointReceiver(XMLmessage msgIn) {
		if (this.mGame.getLastTrickPlay().bombsWithTree(msgIn.getContent().getPlayer(), this.mGame)) {
			setChanged();
			notifyObservers(msgIn);									
		}
		
	}

}
